package top.suven.base.http.admin.config.controller;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.config.model.MqFailInfo;
import top.suven.base.http.admin.config.mqfail.MqFailExecCore;
import top.suven.base.http.admin.config.service.MqFailInfoWebService;
import top.suven.base.http.admin.config.vo.request.MqFailInfoRequestVo;
import top.suven.base.http.admin.config.vo.response.MqFailInfoResponseVo;
import top.suven.base.http.admin.config.vo.valid.ConfigValidGroup;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**   
 * @Title: MqFailInfoWebController.java
 * @Description: MQ消费失败信息的控制服务类
 * @author lixiangling
 * @date   2018/09/06 18:49:45
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *MQ消费失败信息
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/maint/mqFailInfo")
public class MqFailInfoWebController {
    private final static Logger logger = LoggerFactory.getLogger(MqFailInfoWebController.class);
	@Autowired
	private MqFailInfoWebService mqFailInfoWebService;
	@Autowired
	private MqFailExecCore mqFailExecCore;
    /**
     * @Title: 跳转到MQ消费失败信息主界面
     * @Description:mqFailInfoRequestVo @{Link MqFailInfoRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/09/06 18:49:45
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("maint:mqFailInfo_list")
    public String index(){
        return "maint/mqFailInfo_index";
    }
    /**
     * @Title: 获取MQ消费失败信息分页信息
     * @Description:mqFailInfoRequestVo @{Link MqFailInfoRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/09/06 18:49:45
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("maint:mqFailInfo_list")
    public PageData<MqFailInfoResponseVo> list(@Validated(value = {ConfigValidGroup.ConfigQueryGroup.class}) MqFailInfoRequestVo mqFailInfoRequestVo){
        if(mqFailInfoRequestVo.getExecStatus() == -1){
            mqFailInfoRequestVo.setExecStatus(null);
        }
        MqFailInfo mqFailInfo = new MqFailInfo();
        BeanUtil.copyProperties(mqFailInfoRequestVo, mqFailInfo);
        return mqFailInfoWebService.getMqFailInfoPage(mqFailInfo, mqFailInfoRequestVo.getPage(), mqFailInfoRequestVo.getLimit());
    }
   /**
    * @Title: 新增MQ消费失败信息信息
    * @Description:mqFailInfoRequestVo @{Link MqFailInfoRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/09/06 18:49:45
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "compensate")
    @RequiresPermissions("maint:mqFailInfo_compensate")
    public ResponseResult compensate(@RequestParam(value = "id") Long[] ids, String topicName){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }else if(StringUtils.isBlank(topicName)){
            return ResponseResult.error(SysMsgEnumType.SYS_PARAM_ERROR.setMsg(String.format(SysMsgEnumType.SYS_PARAM_ERROR.getMsg(), "标题不能为空")));
        }

        mqFailExecCore.compensate(ids,topicName);
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改MQ消费失败信息信息
    * @Description:mqFailInfoRequestVo @{Link MqFailInfoRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/09/06 18:49:45
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "compensateBatch")
    @RequiresPermissions("maint:mqFailInfo_compensateBatch")
    public ResponseResult compensateBatch(String topicName){
        if(StringUtils.isBlank(topicName)){
            return ResponseResult.error(SysMsgEnumType.SYS_PARAM_ERROR.setMsg(String.format(SysMsgEnumType.SYS_PARAM_ERROR.getMsg(), "标题不能为空")));
        }
        mqFailExecCore.compensateBatch(topicName);
        return ResponseResult.ok();
    }
    /**
     * @Title: 取消补偿MQ消费失败信息信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/09/06 18:49:45
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("cancel")
    @RequiresPermissions("maint:mqFailInfo_cancel")
    public ResponseResult cancel( @RequestParam(value = "id") Long[] ids,String reason){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }else if(StringUtils.isBlank(reason)){
            return ResponseResult.error(SysMsgEnumType.SYS_PARAM_ERROR.setMsg(String.format(SysMsgEnumType.SYS_PARAM_ERROR.getMsg(), "取消原因不能为空")));
        }
        mqFailInfoWebService.cancel(ids,reason);
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看MQ消费失败信息信息
    * @Description:mqFailInfoRequestVo @{Link MqFailInfoRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/09/06 18:49:45
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("maint:mqFailInfo_list")
    public String detail(ModelMap modelMap, Long id){
        MqFailInfo mqFailInfo = null;
        if(id > 0){
            mqFailInfo = mqFailInfoWebService.getMqFailInfo(id);
        }
        modelMap.put("mqFailInfo", mqFailInfo);
        return "maint/mqFailInfo_detail";
    }


    /**
    * @Title: 导出MQ消费失败信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/09/06 18:49:45
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("maint:mqFailInfo_export")
    public void export(HttpServletResponse response, MqFailInfoRequestVo mqFailInfoRequestVo){
        MqFailInfo mqFailInfo = new MqFailInfo();
        BeanUtil.copyProperties(mqFailInfoRequestVo, mqFailInfo);
        PageData<MqFailInfoResponseVo> mqFailInfoPage = mqFailInfoWebService.getMqFailInfoPage(mqFailInfo, mqFailInfoRequestVo.getPage(), mqFailInfoRequestVo.getLimit());
        List<MqFailInfoResponseVo> data = mqFailInfoPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("mqFailId", "MQ消费失败数据编号")
			.add("topicName", "主题名称")
			.add("mqMessage", "消息")
			.add("execStatus", "处理状态")
			.add("reason", "原因")
			.add("createTime", "创建时间")
			.add("modifyTime", "修改时间")
			.add("dealInfo", "处理信息")
			.add("failInfo", "失败信息")
			.add("globalId", "全局唯一标识");

        try {
            ExportUtils.write(String.format("MQ消费失败信息_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}